package cn.jn.lhm.order.handler;

import cn.jn.lhm.order.service.PayLogService;
import cn.jn.lhm.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/15 - 22:38
 */
@RestController
@Slf4j
@RequestMapping("/order/log")
public class PayLogHandler {

    @Resource
    private PayLogService payLogService;

    @GetMapping("/create/native/{orderNum}")
    public R createNative(@PathVariable("orderNum")String orderNum){
        //返回信息，包含二维码信息
        Map<String, Object> map = payLogService.createNative(orderNum);
        return R.ok().data(map);
    }


    @GetMapping("/get/order/status/{orderNum}")
    public R getOrderStatus(@PathVariable("orderNum")String orderNum){
        //查询微信的订单状态，判断是否以及成功支付
        Map<String,String> map = payLogService.getOrderStatus(orderNum);
        if (map != null && "SUCCESS".equals(map.get("trade_state"))){
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.error().message("支付失败");
    }

    /**
     * 远程RPC调用
     * @param userId 用户ID
     * @param courseId 课程ID
     * @return 检测支付成功
     */
    @GetMapping("/get/order/info/{userId}/{courseId}")
    public boolean getOrderInfoToCheckStatus(@PathVariable("userId")String userId,
                                       @PathVariable("courseId")String courseId){
        return payLogService.getOrderInfo(userId, courseId);
    }
}
